using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace The_LarX
{
    /// <summary>
    /// Creates a Button
    /// </summary>
    public class Button : Component
    {
        public Sprite2D OffTex;
        public Sprite2D OnTex;

        public event EventHandler OnClick;

        private bool Intersects;

        /// <summary>
        /// If the button is selected
        /// </summary>
        public bool Selected;

        /// <summary>
        /// If the player can hold the button
        /// </summary>
        public bool Hold;

        /// <summary>
        /// If when player clicks, the Button will change it's Selected State
        /// </summary>
        public bool OnClickChangesSelected = true;

        public Button(Sprite2D OnTex, Sprite2D OffTex, GameScreen Parent)
            : base(Parent)
        {
            this.OnTex = OnTex;
            this.OffTex = OffTex;
#if WINDOWS_PHONE
            TouchPanel.EnabledGestures = GestureType.Tap | TouchPanel.EnabledGestures;
#endif
        }
        public Button(Sprite2D OnTex, GameScreen Parent)
            : base(Parent)
        {
            this.OnTex = OnTex;
            OnClickChangesSelected = false;
            Selected = true;
#if WINDOWS_PHONE
            TouchPanel.EnabledGestures = GestureType.Tap | TouchPanel.EnabledGestures;
#endif
        }

        public override void Update()
        {
            Intersects = false;

            if (Selected)
            {
                OnTex.Update();
            }
            else
            {
                if (OffTex != null)
                    OffTex.Update();
            }
            
            if (Hold)
            {
#if WINDOWS_PHONE
                for (int i = 0; i < Engine.Input.Touches.Count; i++)
                {
                    TouchIntesectButton(Engine.Input.Touches[i].Position);
                }
#else
                if (Engine.Input.MouseLeftDown())
                {
                    TouchIntesectButton(Engine.Input.MousePos());
                }
#endif
            }
            else
            {
#if WINDOWS_PHONE
                for (int i = 0; i < Engine.Input.Gestures.Count; i++)
                {
                    if (Engine.Input.Gestures[i].GestureType == GestureType.Tap)
                        TouchIntesectButton(Engine.Input.Gestures[i].Position);
                }
#else
                if (Engine.Input.MouseLeftClick())
                {
                    TouchIntesectButton(Engine.Input.MousePos());
                }
#endif
            }
            base.Update();
        }

        public void TouchIntesectButton(Vector2 touch)
        {
            Rectangle Rect = new Rectangle((int)touch.X, (int)touch.Y, 1, 1);
            if (Selected)
            {
                if (Rect.Intersects(OnTex.SizeRectangle))
                {
                    if (OnClickChangesSelected)
                        Selected = false;
                    if (OnClick != null)
                        OnClick(this, EventArgs.Empty);
                }
            }
            else
            {
                if (Rect.Intersects(OffTex.SizeRectangle))
                {
                    if (OnClickChangesSelected)
                        Selected = true;
                    if (OnClick != null)
                        OnClick(this, EventArgs.Empty);
                }
            }
        }

        public override void Draw()
        {
            if (Selected)
            {
                OnTex.Draw();
            }
            else
            {
                if (OffTex != null)
                    OffTex.Draw();
            }

            base.Draw();
        }
    }
}
